# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qscintilla
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=2.13.3
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=("spdx:GPL-3.0-only")
url="https://riverbankcomputing.com/software/qscintilla/intro"
makedepends=("${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-pyqt-builder"
             "${MINGW_PACKAGE_PREFIX}-pyqt5-sip"
             "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=(!strip staticlibs !buildflags)
source=(https://www.riverbankcomputing.com/static/Downloads/QScintilla/${pkgver}/QScintilla_src-${pkgver}.tar.gz)
sha256sums=('711d28e37c8fccaa8229e8e39a5b3b2d97f3fffc63da10b71c71b84fa3649398')

build() {
  ## Build for Qt5
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r QScintilla_src-${pkgver} build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  # build src
  cd "${srcdir}/build-${MSYSTEM}/src"
  export QMAKEFEATURES="${srcdir}/build-${MSYSTEM}/src/features/"
  ${MINGW_PREFIX}/bin/qmake
  make PREFIX=${MINGW_PREFIX}

  # build designer
  cd "${srcdir}/build-${MSYSTEM}/designer"
  qmake INCLUDEPATH+=../src QMAKE_LIBDIR+=../src/release
  make PREFIX=${MINGW_PREFIX}

  # build python bindings
  cd "${srcdir}/build-${MSYSTEM}/Python"
  cp pyproject{-qt5,}.toml

  MSYS2_ARG_CONV_EXCL="--api-dir=;" \
  ${MINGW_PREFIX}/bin/sip-build \
    --no-make \
    --qsci-features-dir=../src/features \
    --qsci-include-dir=../src \
    --qsci-library-dir=../src/release \
    --api-dir=${MINGW_PREFIX}/share/qt5/qsci/api/python
  make -C build PREFIX=${MINGW_PREFIX}

}

package_qscintilla() {
  pkgdesc="A port to Qt5 of Neil Hodgson's Scintilla C++ editor class (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base")

  cd "${srcdir}/build-${MSYSTEM}"

  # Fix Makefiles
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|${PREFIX_WIN:0:2}\$(INSTALL_ROOT|\$(INSTALL_ROOT|g" {} \;
  find src designer -type f \( -name 'Makefile*' \) \
      -exec sed -i -e "s|)${PREFIX_WIN:2}|)${MINGW_PREFIX}|g" {} \;

  make -C "src" INSTALL_ROOT="${pkgdir}" install
  make -C "designer" INSTALL_ROOT="${pkgdir}" install

  [[ -d "${pkgdir}"/${MINGW_PREFIX}/bin/ ]] || mkdir -p "${pkgdir}"/${MINGW_PREFIX}/bin/
  mv "${pkgdir}"/${MINGW_PREFIX}/lib/*.dll "${pkgdir}"/${MINGW_PREFIX}/bin/

  install -Dm644 "${srcdir}/build-${MSYSTEM}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python-qscintilla() {
  pkgdesc="Python bindings for QScintilla2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python-pyqt5")
  provides=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
            "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
             "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")
  replaces=("${MINGW_PACKAGE_PREFIX}-python-qscintilla-common"
            "${MINGW_PACKAGE_PREFIX}-python3-qscintilla")

  cd "${srcdir}/build-${MSYSTEM}"
  make -C "Python/build" INSTALL_ROOT="${pkgdir}" install

  install -Dm644 "${srcdir}/build-${MSYSTEM}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
